import React, {Component} from 'react';
import './input.scss';

/**
 * 输入框，可接受 class
 */
class Input extends Component {
    constructor(props) {
        super(props);
        this.change = this.change.bind(this);
    }

    change(e) {
        const val = e.target.value;
        const pattern = /^\d$/;
        if (!pattern.test(val)) {
            e.target.value = '';
        }
    }

    render() {
        return (
            <input type="text" className={`input ${this.props.className}`}
                   readOnly={this.props.readOnly}
                   onChange={this.change}
                   defaultValue={this.props.defaultValue} />
        );
    }
}

export default Input;
